package com.atguigu.edu.service.impl;

import com.atguigu.edu.entity.Chapter;
import com.atguigu.edu.entity.Video;
import com.atguigu.edu.entity.vo.ChapterVideoVo;
import com.atguigu.edu.entity.vo.VideoVo;
import com.atguigu.edu.mapper.ChapterMapper;
import com.atguigu.edu.service.ChapterService;
import com.atguigu.edu.service.VideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author admin
 * @since 2020-07-06
 */
@Service
@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED)
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements ChapterService {

    @Autowired
    VideoService videoService;

    @Override
    public List<ChapterVideoVo> findChapterVideoById(String courseId) {
        QueryWrapper<Chapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
//        chapterList
        List<Chapter> chapterList = baseMapper.selectList(wrapper);
        wrapper = null;
        QueryWrapper<Video> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("course_id", courseId);
//        videoList
        List<Video> videoList = videoService.list(wrapper2);
        wrapper2 = null;

        List<ChapterVideoVo> chapterVideoVoList = new ArrayList<>();

        for (Chapter chapter : chapterList) {
            ChapterVideoVo chapterVideoVo = new ChapterVideoVo();
            BeanUtils.copyProperties(chapter, chapterVideoVo);

//            创建子集合 存储二级菜单
            List<VideoVo> videoVoList = new ArrayList<>();
            for (Video video : videoList) {
                if (!StringUtils.isEmpty(chapter.getId())) {
                    if (chapter.getId().equals(video.getChapterId())) {

                        VideoVo videoVo = new VideoVo();

                        BeanUtils.copyProperties(video, videoVo);
                        videoVoList.add(videoVo);
                        videoVo = null;
                    }
                }
            }
            chapterVideoVo.setVideoVoList(videoVoList);
            chapterVideoVoList.add(chapterVideoVo);
            videoVoList = null;
            chapterVideoVo = null;
        }
//        返回整合后的 chapterVideoVoList对象
        return chapterVideoVoList;
    }

    /**
     * 删除章节信息 如果章节中包含小节 则无法删除
     *
     * @param chapterId
     * @return
     */
    @Override
    public boolean deleteChapterById(String chapterId) {
        QueryWrapper<Video> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id", chapterId);
        int count = videoService.count(wrapper);
        wrapper = null;
        int i;
        if (count <= 0) {
            try {
                i = baseMapper.deleteById(chapterId);
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        } else {
            return false;
        }
        return i > 0;
    }
}
